//
//  EnterpriseProgressDetailViewController.m
//  homeland
//
//  Created by SunShine on 13-11-3.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "EnterpriseProgressDetailViewController.h"
#import "AdvisoryDetailInfoCell.h"

@interface EnterpriseProgressDetailViewController ()
@property (strong, nonatomic) ProgressEnquiryService *dataService;
@property (assign, nonatomic) BOOL onFeedBack;
@end

@implementation EnterpriseProgressDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"办理结果";
    self.dataService = [ProgressEnquiryService new];
    _dataService.delegate = self;
    [self startLoadingView];
    [_dataService requestEnterpriseProgressDetailForKey:_listItem.user_id];
    if ([_listItem.current_link compare:@"结束"]==NSOrderedSame) {
        self.appraiseView.hidden=NO;
    }
    else{
        self.appraiseView.hidden=YES;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 7;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 1) {
        if (_dataService.enterpriseDetailItem.approve_item && _dataService.enterpriseDetailItem.approve_item != (id)[NSNull null] && _dataService.enterpriseDetailItem.approve_item.length > 0){
            CGFloat height = [self caculateHeightWithoutPaddingForString:_dataService.enterpriseDetailItem.approve_item AndFontSize:14.0 AndWitdh:170.0];
            return MAX(44.0, height+26.0);
        }
    }
    return 44.0;
}

//-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
//{
//    return _footer;
//}
//
//-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
//{
//    return CGRectGetHeight(_footer.frame);
//}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AdvisoryDetailInfoCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"AdvisoryDetailInfoCell" owner:self options:nil] objectAtIndex:0];
    EnterpriseProgressDetailItem *item = _dataService.enterpriseDetailItem;
    if (indexPath.row == 0) {
        cell.titleLabel.text = @"业务流水号";
        cell.detailTextView.text = item.control_seq;
    } else if (indexPath.row == 1) {
        cell.titleLabel.text = @"事项名称";
        cell.detailTextView.text = item.approve_item;
    } else if (indexPath.row == 2) {
        cell.titleLabel.text = @"事项编号";
        cell.detailTextView.text = item.item_code;
    } else if (indexPath.row == 3) {
        cell.titleLabel.text = @"办理状态";
        cell.detailTextView.text = _listItem.current_link;
    } else if (indexPath.row == 4) {
        cell.titleLabel.text = @"受理时间";
        if (item.accept_date && item.accept_date != (id)[NSNull null]) {
            cell.detailTextView.text = item.accept_date;
        }
    } else if (indexPath.row == 5) {
        cell.titleLabel.text = @"承诺时间";
        if (item.announce_finish_time && item.announce_finish_time != (id)[NSNull null]) {
            cell.detailTextView.text = item.announce_finish_time;
        }
    } else if (indexPath.row == 6) {
        cell.titleLabel.text = @"承诺时限";
        cell.detailTextView.text = item.finish_time_limit;
    }
    cell.detailTextView.contentInset = UIEdgeInsetsMake(-8, -8, -8, -8);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark - Data Service Delegate
-(void)loadDataFinished
{
    [self stopLoadingView];
    [_tableView reloadData];
    if (_dataService.enterpriseDetailItem.vote_grade && _dataService.enterpriseDetailItem.vote_grade != (id)[NSNull null]) {
        //已经投过票了
        _button1.enabled = NO;
        _button2.enabled = NO;
        _button3.enabled = NO;
        NSInteger vote = _dataService.enterpriseDetailItem.vote_grade.integerValue;
        if (vote == 1) {
            _image1View.image = [UIImage imageNamed:@"RadioButton-selected.png"];
        } else if (vote == 2) {
            _image2View.image = [UIImage imageNamed:@"RadioButton-selected.png"];
        } else if (vote == 3) {
            _image3View.image = [UIImage imageNamed:@"RadioButton-selected.png"];
        }
    }
    if (self.onFeedBack) {
        _button1.enabled = NO;
        _button2.enabled = NO;
        _button3.enabled = NO;
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"反馈成功." delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}

-(void)loadDataFaild:(NSString *)msg
{
    _button1.enabled = NO;
    _button2.enabled = NO;
    _button3.enabled = NO;
    [self stopLoadingView];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"错误" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}

#pragma mark - Value Buttons Action
- (IBAction)button1Action:(id)sender
{
    self.onFeedBack = YES;
    _image1View.image = [UIImage imageNamed:@"RadioButton-selected.png"];
    _image2View.image = [UIImage imageNamed:@"RadioButton-Unselected.png"];
    _image3View.image = [UIImage imageNamed:@"RadioButton-Unselected.png"];
    [_dataService feedbackEnterpriseProgressWithID:_dataService.enterpriseDetailItem.Id AndVote:1];
    [self startLoadingViewWithText:@"正在提交..."];
}

- (IBAction)button2Action:(id)sender
{
    self.onFeedBack = YES;
    _image1View.image = [UIImage imageNamed:@"RadioButton-Unselected.png"];
    _image2View.image = [UIImage imageNamed:@"RadioButton-selected.png"];
    _image3View.image = [UIImage imageNamed:@"RadioButton-Unselected.png"];
    [_dataService feedbackEnterpriseProgressWithID:_dataService.enterpriseDetailItem.Id AndVote:2];
    [self startLoadingViewWithText:@"正在提交..."];
}

- (IBAction)button3Action:(id)sender
{
    self.onFeedBack = YES;
    _image1View.image = [UIImage imageNamed:@"RadioButton-Unselected.png"];
    _image2View.image = [UIImage imageNamed:@"RadioButton-Unselected.png"];
    _image3View.image = [UIImage imageNamed:@"RadioButton-selected.png"];
    [_dataService feedbackEnterpriseProgressWithID:_dataService.enterpriseDetailItem.Id AndVote:3];
    [self startLoadingViewWithText:@"正在提交..."];
}
@end
